package com.yeyks.controller;

import com.yeyks.dal.dao.FeedbackMapper;
import com.yeyks.dal.domain.AccountInfo;
import com.yeyks.dal.domain.Feedback;
import com.yeyks.exception.PermissionInvalidException;
import com.yeyks.exception.TokenInvalidException;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.model.em.AccountType;
import com.yeyks.model.em.ResponseStatus;
import com.yeyks.service.AccountService;
import com.yeyks.exception.PermissionInvalidException;
import com.yeyks.exception.TokenInvalidException;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.model.em.AccountType;
import com.yeyks.model.em.ResponseStatus;
import com.yeyks.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/ajax/feedback")
public class FeedbackController {
    @Autowired
    AccountService accountService;

    @Autowired
    FeedbackMapper feedbackMapper;

    /**
     * 根据指定的条件查询反馈信息
     * @param request
     * @param response
     * @param token
     * @param gmtStart
     * @param gmtEnd
     * @param user
     * @param pageNum
     * @param pageSize
     * @param feedback
     * @return
     */
    @ApiIgnore
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResponseEntity<?> list(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            @RequestParam(name = "gmtStart", defaultValue = "", required = true) String gmtStart,
            @RequestParam(name = "gmtEnd", defaultValue = "", required = true) String gmtEnd,
            @RequestParam(name = "user", defaultValue = "", required = true) String user,
            @RequestParam(name = "pageNum", defaultValue = "", required = true) Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "", required = true) Integer pageSize,
            Feedback feedback
    ) {
        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {
            AccountInfo accountInfo = accountService.token2AccountInfo(token);
//            if (accountInfo.getType() > AccountType.Administrator) {
//                throw new PermissionInvalidException();
//            }
            if (accountInfo.getType() != AccountType.Administrator) {
                throw new PermissionInvalidException();
            }

            Date dtStart = gmtStart.isEmpty() ? null : new Date(Long.parseLong(gmtStart));
            Date dtEnd = gmtEnd.isEmpty() ? null : new Date(Long.parseLong(gmtEnd));

            if (feedback.getReport() == null) {
                feedback.setReport(false);
            }

            List<Feedback> feedbackList = feedbackMapper.selectByDateNameReaded(
                    dtStart, dtEnd, user,
                    feedback.getReaded(), feedback.getReport(),
                    (pageNum != null && pageSize != null) ? (pageNum - 1) * pageSize : null,
                    pageSize);
            int count = feedbackMapper.selectByDateNameReadedCount(
                    dtStart, dtEnd, user,
                    feedback.getReaded(), feedback.getReport());

            responseBody.addMapBody("list", feedbackList);
            responseBody.addMapBody("count", count);

            responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
            responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        } catch (PermissionInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PERMISSION.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_PERMISSION.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(responseBody);
    }

    /**
     * 将反馈修改为已读
     * @param request
     * @param response
     * @param token
     * @param feedback
     * @return
     */
    @ApiIgnore
    @RequestMapping(value = "/markReaded", method = RequestMethod.POST)
    public ResponseEntity<?> markReaded(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            Feedback feedback
    ) {
        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {
            AccountInfo accountInfo = accountService.token2AccountInfo(token);
//            if (accountInfo.getType() > AccountType.Administrator) {
//                throw new PermissionInvalidException();
//            }
            if (accountInfo.getType() != AccountType.Administrator) {
                throw new PermissionInvalidException();
            }

            int rec = feedbackMapper.markReaded(feedback.getId());
            if (rec == 0) {
                throw new Exception("updated 0 row.");
            }

            responseBody.setBody(rec);

            responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
            responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        } catch (PermissionInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PERMISSION.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_PERMISSION.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }

        return ResponseEntity.ok(responseBody);
    }

    /**
     * 增加一条反馈（前端传content, report = false, token, ）
     * @param request
     * @param response
     * @param token
     * @param feedback
     * @return
     */
    @ApiIgnore
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity<?> add(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(name = "token", defaultValue = "", required = true) String token,
            Feedback feedback
    ) {
        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {
            AccountInfo accountInfo = accountService.token2AccountInfo(token);
            if (accountInfo.getType() != AccountType.User) {
                throw new PermissionInvalidException();
            }

            feedback.setUserId(accountInfo.getId());
            feedback.setUserName(accountInfo.getUsername());
            feedback.setUserPhone(accountInfo.getPhone());
            feedback.setReaded(false);

            int rec = feedbackMapper.insertReturnId(feedback);
            if (rec == 0) {
                throw new Exception("updated 0 row.");
            }

            responseBody.setBody(rec);

            responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
            responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        } catch (TokenInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_TOKEN.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_TOKEN.getMsg());
        } catch (PermissionInvalidException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PERMISSION.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL_INVALID_PERMISSION.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(e.getMessage());
        }

        return ResponseEntity.ok(responseBody);
    }
}
